<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Secondary Tables</title>
</head>
<body>
  <h3>Partitioning Data - Using Secondary Tables</h3>
  If required, a single entity can be mapped to multiple tables using <em>@SecondaryTable</em> or
  <em>@SecondaryTables</em> annotation at the class level. The default table or the table
defined by the @Table annotation is termed as the primary table and any additional ones as secondary tables
for the entity. The data in the entity can then be split up across rows in both the primary table and
secondary tables by specifying the table name on each field or property using <em>@Column</em> or <em>@JoinColumn</em>
annotations. The primary and secondary tables are joined using the primary key of the primary table. You can customize
the join column name in secondary tables using <em>@PrimaryKeyJoinColumn</em> annotation as shown below -
  <pre class="brush: java">
@SecondaryTables({
        @SecondaryTable(name="basic_sec_tab_emp_phone"),
        @SecondaryTable(name="basic_sec_tab_emp_email", 
            pkJoinColumns=@PrimaryKeyJoinColumn(name="emp_id"))
})
public class Employee implements Serializable {
...
  </pre>
  
  <h3>Using Secondary Tables for Embedded Classes</h3>
  Secondary tables, many times, is a great choice for mapping embedded classes into separate tables.
  The Embedded class is mapped by the usual <em>@Embedded</em> annotation, however, each embedded class
  column mapping needs to be overridden to assign it to secondary table using <em>@AttributeOverrides</em>
  and <em>@AttributeOverride</em> annotations as shown below -
  <pre class="brush: java">
@Entity(name="SecondaryTableEmbeddedEmployee")
@Table(name="basic_sec_tab_emb_emp")
@SecondaryTable(name="basic_sec_tab_emb_emp_addr", 
            pkJoinColumns=@PrimaryKeyJoinColumn(name="emp_id"))
public class Employee implements Serializable {
	private int id;
	private String name;
	private Address address;
	
	public Employee() {}

	public Employee(String name, Address address) {
		this.name = name;
		this.address = address;
	}

	@Id @GeneratedValue
	public int getId() {
		return id;
	}

	private void setId(int id) {
		this.id = id;
	}

	@Column(nullable = false, length = 30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Embedded
	@AttributeOverrides({
		@AttributeOverride(name="street", column=@Column(table="basic_sec_tab_emb_emp_addr")),
		@AttributeOverride(name="city", column=@Column(table="basic_sec_tab_emb_emp_addr")),
		@AttributeOverride(name="state", column=@Column(table="basic_sec_tab_emb_emp_addr")),
		@AttributeOverride(name="zipCode", column=@Column(name="ZIP", table="basic_sec_tab_emb_emp_addr"))
	})
	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}	
}    
  </pre>
</body>
</html>
